using System.Drawing;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ruminate.GUI.Framework;
using Ruminate.Utils;
using Color = Microsoft.Xna.Framework.Color;
using Point = Microsoft.Xna.Framework.Point;
using Rectangle = Microsoft.Xna.Framework.Rectangle;

namespace Ruminate.GUI.Content {

    public class SlidingDoorRenderer : Renderer {

        State DrawState { get; set; }

        public int Height { get; set; }
        public int CenterWidth { get; set;}
        public int EndWidth { get; set; }
        public int Background { get; set; }
        public int Depth { get; set; }

        public SlidingDoorRenderer(Texture2D imageMap, int height, int centerWidth, int endWidth, 
            int background, int depth, RuminateGUI gui) : base(gui, imageMap) {

            Height = height;
            CenterWidth = centerWidth;
            EndWidth = endWidth;
            Background = background;
            Depth = depth;

            DrawState = State.Default;
        }

        private static Texture2D RenderLayer(SlidingDoorRenderer sdR, Rectangle edge,
            Rectangle center, Point size, GraphicsDevice graphicsDevice) {

            var baker = new TextureBaker(graphicsDevice, size.ToVector2(), RenderState.Fill);

            //Create destination rectangles
            # region Regions to draw to
            var leftArea = new Rectangle(0, 0, sdR.EndWidth, sdR.Height);
            var rightArea = new Rectangle(size.X - sdR.EndWidth, 0, sdR.EndWidth, sdR.Height);
            var centerArea = new Rectangle(sdR.EndWidth, 0, size.X - (sdR.EndWidth * 2), sdR.Height);
            #endregion

            //Edges
            baker.Draw(
                sdR.ImageMap,
                leftArea,
                edge,
                Color.White,
                0.0f,
                Vector2.Zero,
                SpriteEffects.FlipHorizontally,
                0);
            baker.Draw(
                sdR.ImageMap,
                rightArea,
                edge,
                Color.White,
                0.0f,
                Vector2.Zero,
                SpriteEffects.None,
                0);
            baker.Draw(
                sdR.ImageMap,
                centerArea,
                center,
                Color.White);

            return baker.GetTexture();
        }

        public Texture2D Render(Point size, int offset) {

            Rectangle
                edge = new Rectangle(offset * EndWidth, 0, EndWidth, Height),
                center = new Rectangle(offset * CenterWidth, Height, CenterWidth, Height),
                sEdge = new Rectangle(Depth * EndWidth , 0, EndWidth, Height),
                sCenter = new Rectangle(Depth * CenterWidth, Height, CenterWidth, Height),
                background = new Rectangle(0, Depth * Height, Background, Background);

            var overlay = RenderLayer(this, edge, center, size, GraphicsDevice);
            var stencil = RenderLayer(this, sEdge, sCenter, size, GraphicsDevice);

            var baker = new TextureBaker(GraphicsDevice, new Vector2(Background, Background), RenderState.Fill);
            baker.Draw(ImageMap, Vector2.Zero, background, Color.White);
            var sBackground = baker.GetTexture(RotateFlipType.RotateNoneFlipNone);

            baker = new TextureBaker(GraphicsDevice, size.ToVector2(), RenderState.Fill);

            RenderStencilandOverlay(baker, ImageMap, sBackground, stencil, overlay, size);

            return baker.GetTexture();
        }
    }
}
